package com.yd.app.service.pc.jewellery.jewelleryPageService;

import com.yd.app.mapper.jewellery.jewelleryPageMapper.WebSiteGoodsMapper;
import com.yd.app.service.common.BaseService;
import com.yd.domain.business.*;
import com.yd.domain.commom.PageModel;
import com.yd.domain.searchParam.GoodsSearchParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zy
 * @create 2018-05-14 9:15
 * @desc 电脑前端商品展示
 **/
@Service
public class WebSiteGoodsService extends BaseService{
    @Autowired
    WebSiteGoodsMapper goodsMapper;
    /**
     * 查询商品列表
     * @param param
     * @param pageIndex
     * @param pageSize
     * @return
     */
    public PageModel goodsList(GoodsSearchParam param, int pageIndex, int pageSize) {
        PageModel pm=new PageModel();
        List<Goods> returnList=goodsMapper.getGoodsList(param,getStartRow(pageIndex,pageSize),pageSize);
        int count=0;
        if(pageIndex==1){
            count=goodsMapper.getGoodsListCount(param);
        }
        pm.setCount(count);
        pm.setList(returnList);
        return  pm;
    }
    /**
     * @Author: zy
     * @Desc: 查询商品分类情况
     * @Date:  2018/5/14
     * @Param
     */
    public List<GTypeOne> getGoodNav() {
        List<GTypeOne> list=goodsMapper.getTypeOne();
        for(GTypeOne g :list){
            List<GType> gTypeList=goodsMapper.getGTypeList(g.getId());
            if(gTypeList!=null){
                g.setTypeList(getGList(gTypeList));
            }
        }
        return list;
    }
    /**
     * @Author: zy
     * @Desc: 二级查询三级
     * @Date:  2018/4/28
     * @Param
     */
    private List<GType> getGList(List<GType> list){
        for(GType g :list){
            g.setStyleList(getStyleList(g.getId()));
        }
        return list;
    }

    /**
     * @Author: zy
     * @Desc: 查询第三级,三级查询了四级
     * @Date:  2018/4/28
     * @Param
     */
    public List<GStyle> getStyleList(String pId){
        List<GStyle> l=goodsMapper.getGStyleList(pId);
        if(l==null){
            l=new ArrayList<>();
        }else{
            for(GStyle g :l){
                g.setpList(goodsMapper.getPListByPId(g.getId()));
            }
        }
        return l;
    }
    /**
     * 查询商品相信信息
     * @param goodsId
     * @return
     */
    public Goods getGoodsById(String goodsId) {
        Goods goods=goodsMapper.getGoodsById(goodsId);
        List<GoodsWeight>goodsWeightList=goodsMapper.getGoodsWeight(goodsId);
        if(goodsWeightList!=null){
            goods.setGoodsWeightList(goodsWeightList);
        }
        return goods;
    }
    /**
     * @Author: zy
     * @Desc: 分页查询商品评价信息
     * @Date:  2018/5/14
     * @Param
     */
    public PageModel getEvaluateList(String goodsId, int pageIndex, int pageSize) {
        PageModel pm = new PageModel();
        int startRow = getStartRow(pageIndex, pageSize);
        List<Evaluate> evaList = goodsMapper.getEvaluateList(goodsId, startRow, pageSize);
        int count = 0;
        if (pageIndex == 1) {
            //查询总条数
            count = goodsMapper.getEvaluateCount(goodsId);
            pm.setCount(count);
        }
        pm.setList(evaList);
        return pm;
    }

    /**
     * @Author: zy
     * @Desc: 获取商品所有的相关图片
     * @Date:  2018/5/14
     * @Param
     */
    public List<Pic> getAllPic(String goodId){
        List<Pic> list=new ArrayList<>();
        list=goodsMapper.getAllPic(goodId);
        return list;
    }
}
